<? include ("../init.php") ?>
<?
  $location = $_POST['location'];
  $detail = $_POST['detail'];
  
  $sql = "INSERT INTO showcase(showcase_place, showcase_detail) VALUES('$location','$detail')";

  $db->execute($sql);
  
  if(isset($_FILES["showcase_img"]) && is_uploaded_file($_FILES["showcase_img"]["tmp_name"]) && $_FILES["showcase_img"]["tmp_name"]!=''){
    $showcase_id = $db->lastInsertedId();
    
    $image = new image();
  
    ini_set("html_errors", "0");
    
    // Check the upload
    if ($_FILES["showcase_img"]["error"] != 0) {
      echo "ERROR:invalid upload";
      exit(0);
    }
  
    $name = get_file_name($showcase_id, $_FILES['showcase_img']['tmp_name']);
    $path = "../../images/showcase/".$showcase_id;
    mkdir($path);
    $urlpath = get_url_path($path);
    $link = URL."/".$urlpath."/".$name;
    $filename = $path ."/". $name;
  
    if (!move_uploaded_file($_FILES['showcase_img']['tmp_name'], $filename)){
      echo 'Problem: Could not move file to destination directory';
      exit(0);
    }
  
    ini_set('memory_limit', '100M');
    $image->load($filename);
    $image->resize(128,85);
    $image->save($filename);
    ini_set('memory_limit', '16M');
  
    chmod($filename,0755);
    
    $sql = "UPDATE showcase SET showcase_img ='$name' WHERE showcase_id=$showcase_id";
    $db->execute($sql);
  }
  
  header("Location:index.php");
?>